package com.bawei.shopping2.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.shopping2.databinding.ItemGoodsBinding
import com.bawei.shopping2.entity.CategoryEntity
import com.bawei.shopping2.entity.GoodsEntity

class GoodsAdapter(var list: MutableList<GoodsEntity>): RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder>() {
    class GoodsViewHolder(var itemGoodsBinding: ItemGoodsBinding) :RecyclerView.ViewHolder(itemGoodsBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
       var itemGoodsBinding = ItemGoodsBinding.inflate(LayoutInflater.from(parent.context),parent,false)
       return GoodsViewHolder(itemGoodsBinding)
    }

    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
       holder.itemGoodsBinding.goods = list[position]
        //item的点击事件，lambda
        holder.itemView.setOnClickListener {
            onItemClick(it,position)
        }
    }

    override fun getItemCount(): Int {
       return list.size
    }
    //lambda表达式替换java中接口回调
    lateinit var onItemClick:(v: View, pos: Int) -> Unit   //函数类型 参数是view：position 无返回值
    //根据下标返回对应的数据
    fun getDataByPosition(position:Int): GoodsEntity {
        return list[position]
    }
}